﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace mojoPortal.Web.RS.SBM.Controls
{
    public partial class ModalViewCheckoutReturnEquipControl : RSBaseUserControl
    {

        
        protected void Page_Load(object sender, EventArgs e)
        {
            
            
        }

        public void ShowModalCheckoutReturnEquip(bool isreturned)
        {
            if (isreturned)
                lblCheckoutReturnEquipHeader.Text = "Equipment Returned";
            else
                lblCheckoutReturnEquipHeader.Text = "Equipment Checked Out";

            RSUserIdentity userIdentity = GetRSUserIdentity();
            gvCheckoutReturnEquip.DataSource = GetCheckoutReturnEquipDataSet(userIdentity.RSCompanyId, isreturned);
            gvCheckoutReturnEquip.DataBind();
            mpeModalCheckoutReturnEquip.Show();
        }

        protected void btnModalOK_Click(Object sender, EventArgs e)
        {
            mpeModalCheckoutReturnEquip.Hide();
        }

        private DataSet GetCheckoutReturnEquipDataSet(int companyid, bool isreturned)
        {
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            String sqlSELECT = "select pe.WorkOrderproductid, pe.productid, pe.quantity as checkoutqty, pe.returnqty, pe.isreturned, " +
                                "i.productname, i.ModelNo, i.productcode, p.WorkOrderid, p.locationid, p.startdt, p.assignto, " + 
                                "(select statusname from rs_tblStatus s where s.StatusId=p.WorkOrderstatusid) WorkOrderstatus " +
                                "from rs_tblWorkOrderequip pe " + 
                                "inner join rs_tblWorkOrder p on pe.workorderid=p.WorkOrderid " + 
                                "inner join rs_tblInventory i on pe.ProductId=i.ProductId " + 
                                "where pe.isreturned=@isreturned and pe.companyid=@companyid order by i.productname";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Bit;
            param.ParameterName = "@isreturned";
            param.Value = isreturned;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            myConnection.Close();

            return ds;
        }
    }
}